import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/resources.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/medecine/medicine_handover/medicine_handover_list_view_model.dart';
import 'package:ls_work/widgets/text_field.dart';

class MedicineHandoverListPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _MedicineHandoverListState();
}

class _MedicineHandoverListState extends BaseMvvmListState<
    MedicineHandoverListViewModel, MedicineHandoverListPage> {
  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: AppBar(
        title: Text('药品移交'),
        actions: [
          IconButton(
              onPressed: () {
                key.currentState?.openEndDrawer();
              },
              icon: Icon(Icons.search))
        ],
      ),
      key: key,
      endDrawer: endDraw(),
      body: Column(
        children: [
          loadingContentWidget.setWeight(1),
          SizedBox(
            height: 10,
          ),
          TextButton(
            onPressed: () {
              viewModel.addHandover();

            },
            child: CommonText('新增', textColor: Colors.white),
            style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(Colors.blue),
                shape: WidgetStateProperty.all(StadiumBorder()),
                minimumSize: WidgetStateProperty.all(
                    Size(MediaQuery.of(context).size.width - 32, 45))),
          ),
          SizedBox(
            height: 20,
          ),
        ],
      ),
    );
  }

  Widget endDraw() {
    return Column(
      children: [
        AppBar(
          leading: Container(),
          actions: [Container()],
          title: Text('筛选'),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 12,
            ),
            CommonText('长者姓名'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                    hintText: '输入长者姓名',
                    text: vm?.searchName ?? "",
                    maxLines: 1,
                    textInputAction: TextInputAction.search,
                    onChanged: (str) {
                      viewModel.searchName = str;
                    },
                    onSubmitted: (str) {
                      viewModel.requestRefresh();
                    })
                .addToContainer(
                    decoration: BoxDecoration(
                        color: Colours.bg_color, borderRadius: 6.borderRadius),
                    padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(
              height: 10,
            ),
            CommonText('房间号'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入房间号',
                text: vm?.roomName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.roomName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),
            SizedBox(
              height: 10,
            ),
            CommonText('药品名称'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                    hintText: '输入药品名称',
                    text: vm?.medicineName ?? "",
                    maxLines: 1,
                    textInputAction: TextInputAction.search,
                    onChanged: (str) {
                      viewModel.medicineName = str;
                    },
                    onSubmitted: (str) {
                      viewModel.requestRefresh();
                    })
                .addToContainer(
                    decoration: BoxDecoration(
                        color: Colours.bg_color, borderRadius: 6.borderRadius),
                    padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),
            SizedBox(
              height: 10,
            ),
            CommonText('操作时间'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.handleDate, hintText: '请选择')
                .addToContainer(
                    width: double.infinity,
                    decoration: BoxDecoration(
                        color: Colours.bg_color, borderRadius: 6.borderRadius),
                    padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5))
                .onTap(() {
              viewModel.chooseHandleDate();
            }),
            SizedBox(
              height: 10,
            ),
            CommonText('操作人'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                    hintText: '输入操作人姓名',
                    maxLines: 1,
                    onChanged: (str) {
                      viewModel.handleName = str;
                    },
                    textInputAction: TextInputAction.search,
                    onSubmitted: (str) {
                      viewModel.requestRefresh();
                    })
                .addToContainer(
                    width: double.infinity,
                    decoration: BoxDecoration(
                        color: Colours.bg_color, borderRadius: 6.borderRadius),
                    padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),
            SizedBox(
              height: 10,
            ),
          ],
        ).addToContainer(
          padding: EdgeInsets.symmetric(horizontal: 16),
        ),
        SizedBox().setWeight(1),
        Gaps.dividerLine,
        SizedBox(
          height: 10,
        ),
        Row(
          children: [
            SizedBox(
              width: 16,
            ),
            TextButton(
              onPressed: () {
                key.currentState?.closeEndDrawer();
                viewModel.resetClick();
              },
              child: CommonText('重置', textColor: Colors.blue),
              style: ButtonStyle(
                  shape: WidgetStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.horizontal(left: 20.radius),
                      side: BorderSide(color: Colors.blue)))),
            ).setWeight(1),
            TextButton(
                onPressed: () {
                  key.currentState?.closeEndDrawer();
                  viewModel.requestRefresh();
                },
                child: CommonText('确定', textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor: WidgetStateProperty.all(Colors.blue),
                    shape: WidgetStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.horizontal(right: 20.radius),
                      side: BorderSide(color: Colors.blue),
                    )))).setWeight(1),
            SizedBox(
              width: 16,
            ),
          ],
        ),
        SizedBox(
          height: 12,
        )
      ],
    ).addToContainer(
        width: MediaQuery.of(context).size.width * 0.7,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.horizontal(left: 12.radius)));
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            CommonText('${item.customerName??""}', textSize: 17, fontWeight: FontWeight.bold),
            SizedBox(
              width: 10,
            ),
            CommonText('${item.bedNo??""}', textSize: 15),
          ],
        ),
        CommonText('${item.drugName??""}',
            textColor: Colors.blue, fontWeight: FontWeight.bold, textSize: 18),
        CommonText('药品规格：${item.specifications??""}'),
        CommonText('最小单位（配药单位）：${item.minUnitName??""}'),
        Row(
          crossAxisAlignment: CrossAxisAlignment.end,
          children: [
            CommonText('移交数量：'),
            SizedBox(
              width: 10,
            ),
            CommonText('${item.transferQuantity?.d}${item.minUnitName}',
                textSize: 17,
                fontWeight: FontWeight.bold,
                textColor: Colors.blue),
          ],
        ),
        CommonText('移交原因：${item.transferReason??""}'),
        CommonText('操作人：${item.executorName??""}'),
        CommonText('操作时间：${item.executorTime??""}'),
      ].insertWidget(SizedBox(
        height: 10,
      )),
    ).addToContainer(
        decoration:
            BoxDecoration(color: Colors.white, borderRadius: 6.borderRadius),
        padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10));
  }

  @override
  MedicineHandoverListViewModel createViewModel() {
    return MedicineHandoverListViewModel(context);
  }

  @override
  EdgeInsets get listPadding =>
      EdgeInsets.symmetric(horizontal: 16, vertical: 12);

  @override
  Widget get separatorDivider => SizedBox(
        height: 12,
      );

  @override
  bool get canPullUp => true;
}
